﻿namespace Pap.Web.Modules.Membership.ViewModel
{
    using System.Collections.Generic;
    using System.Linq;
    using Pap.Data;
    using Pap.Web.Utils;

    /// <summary>
    /// 
    /// </summary>
    public class UserLoginViewModel : IViewModel
    {
        /// <summary>
        /// local variable for membership repository
        /// </summary>
        private IMembershipRepository _MembershipRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserLoginViewModel"/> class.
        /// </summary>
        /// <param name="membershipRepository">The membership repository.</param>
        public UserLoginViewModel(IMembershipRepository membershipRepository)
        {
            this._MembershipRepository = membershipRepository;
        }

        /// <summary>
        /// Logs the user in.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public bool LogUserIn(string userName, string password, out IUser user)
        {
            IEnumerable<IUser> users = _MembershipRepository.FindUsers(u => u.Username.ToLower() == userName.ToLower() && u.Password == password);
            if (users.Count() == 0)
            {
                user = null;
                return false;
            }
            else
            {
                user = users.First();
                return true;
            }
        }
    }
}
